package com.cs.study.homework._0306;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo2 {
    public static void main(String[] args) {
        new CopyFile(new File("E:\\a\\a.txt"), new File("E:\\b\\a.txt")).start();
        new CopyFile(new File("E:\\a\\b.txt"), new File("E:\\b\\b.txt")).start();
        new CopyFile(new File("E:\\a\\c.txt"), new File("E:\\b\\c.txt")).start();

    }
}

class CopyFile extends Thread {
    File oldFile;
    File copyFile;

    public CopyFile(File oldFile, File copyFile) {
        this.oldFile = oldFile;
        this.copyFile = copyFile;
    }

    @Override
    public void run() {
        try (FileOutputStream out = new FileOutputStream(copyFile);
             FileInputStream in = new FileInputStream(oldFile)) {
            int readCount = in.read();
            byte[] bytes = new byte[readCount];

            long length = oldFile.length();
            double temp = 0;
            while ((readCount = in.read()) != -1) {
                out.write(bytes);
                temp += readCount;
                double percent = temp / length;
                System.out.println("文件已传输" + String.format("%.2f",percent) + "%");
            }

        } catch (IOException e) {
            try {
                throw new IOException();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
